package com.itheima.test5;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ServiceDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(10000);
        ExecutorService service = Executors.newCachedThreadPool();
        while (true) {
            Socket accept = socket.accept();
            service.submit(new Runnable() {
                @Override
                public void run() {
                    try {
                        //网络读
                        InputStream is = accept.getInputStream();
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        //本地写
                        String s = UUID.randomUUID().toString();
                        BufferedWriter bww = new BufferedWriter(new FileWriter("day16\\" + s + ".txt"));
                        String ln;
                        Thread.sleep(5000);
                        while ((ln = br.readLine()) != null) {
                            bww.write(ln);
                            bww.flush();
                        }
                        bww.close();
                        //网络写
                        OutputStream os = accept.getOutputStream();
                        OutputStreamWriter osw = new OutputStreamWriter(os);
                        BufferedWriter bw = new BufferedWriter(osw);
                        String len = "上传成功";
                        bw.write(len);
                        bw.flush();
                        accept.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        // socket.close();
        //
//

    }
}
